<?php
namespace app\index\controller;

use think\Controller;
use app\admin\model\AuthRule;
use app\index\model\TokenUser;
use app\index\model\Project;
use app\index\model\Notice;
use app\index\model\Lookinto;

class Common extends Controller
{
    /**
     * 基础控制器初始化
     */
    public function _initialize()
    {
        define('MODULE_NAME', request()->module());
        define('CONTROLLER_NAME', request()->controller());
        define('ACTION_NAME', request()->action());
        
        $box_is_pjax = $this->request->isPjax();
        $this->assign('box_is_pjax', $box_is_pjax);
		
		$treeMenu = $this->GetMenu();
		$this->assign('treeMenu',$treeMenu);
		
		$this->ProjectInt();
		
		if(session('UID')){
			$this->restLogin();
			$noticeModel = new Notice();
			$message = $noticeModel->where(['uid'=>session('UID'),'isread'=>0])->count();
			$this->assign('message', $message);

			//添加逻辑使用户登录后 必须强制去填写调查问卷
			$lookModel = new Lookinto();
			$isLookInto = $lookModel->where(['uid'=>session('UID')])->field('create_time,score')->order('create_time desc')->limit(1)->find();
			$isLookInto = empty($isLookInto) ? array() : $isLookInto->toArray();
			if(empty($isLookInto)){
				$this->assign('wantToLookinto',1);
			}else{
				$this->assign('wantToLookinto',0);
				$this->assign('LookIntoScore',$isLookInto['score']);
			}
		}
    }
	/*
	* 网页导航
	*/
	public function GetMenu(){
		
		$treeMenu = cache('DB_TREE_MENU_PC');
		if(!$treeMenu){
			$aModel = new AuthRule();
			$where = [
				  'ismenu' => 1,
				  'status' => 1,
				  'module' => 'member',
				  'level'  => 1,
			];
			$where2 = [
				  'ismenu' => 1,
				  'status' => 1,
				  'module' => 'member',
			];
			$treeMenu = $aModel->where($where)->order('sorts asc')->select();
			foreach($treeMenu as &$tree){
				$where2['pid'] = $tree['id'];
				$tree['child'] = $aModel->where($where2)->order('sorts asc')->select()->toArray();
			}
			unset($tree);
			cache('DB_TREE_MENU_PC',$treeMenu);
		}
		return $treeMenu;
	}
	
	//项目初始化
	private function ProjectInt(){
		$ProModel = new Project();
		$nowTime = time();
		//预热项目
		$ProModel->where(['start_time'=>['lt',$nowTime],'status'=>['eq',1]])->update(['status'=>2]);
		//认投中项目
		$ProList = $ProModel->where(['status'=>2])->select();
		if($ProList){
			foreach($ProList as $Pro){
				$money = GetProjectBar($Pro['id']);
				if($Pro['end_time'] <= $nowTime && $money < $Pro['target_code']){
                    $cha=$Pro['target_code']-$money;
					$data['status'] = -1;
					//$data['basic_code']=$cha;
                    //$ProModel->where(['id'=>$Pro['id']])->setInc('basic_code',$cha);
					$ProModel->where(['id'=>$Pro['id']])->update($data);
				}
				if($money >= $Pro['target_code'] ){
					$data['status'] = 3;
					$ProModel->where(['id'=>$Pro['id']])->update($data);
				}
				
			}
		}
		
	}
	
    private function restLogin()
    {
        $now_token = session('member_token');   //当前token
        $tkModel = new TokenUser();
        $db_token = $tkModel->where(['uid'=>session('UID'), 'type'=>'2'])->find();   //数据库token
        if ($db_token['token'] != $now_token){   //其他地方登录
			session('UID',null);
			session('userName',null);
			session('mobile',null);
			session('member_token',null);
			return redirect('Login/index');
        }
        return;
    }


	
}